package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoLiveLAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.LiveListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.yizhibo.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 沐诺 on 2017/4/9.
 * 更多直播列表
 */

public class VideoLiveLFragment extends BaseFragment {
    private View cv = null;
    private Context mContext;
    private PullToRefreshListView mPullToRefreshListView;
    private int mTuijianListTaskId = -1;
    private int size = 20;
    private int currentPage = 1;
    private String versionCode;
    private String from;
    private NoDataUtil mNoData;
    private VideoLiveLAdapter mAdapter;
    private String liveType;


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        cv = inflater.inflate(R.layout.fragment_videolivel, null);
        mContext = getActivity();
        Intent intent = getActivity().getIntent();
        from = intent.getStringExtra(ProtocolConst.MORE_FROM);
        if(from != null && from.equals(ProtocolConst.FORM_FIRST)) {
            liveType = intent.getStringExtra("TYPEID");
        }
        initview();
        getTuijianList();
        return cv;
    }

    private void initview() {
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.videolistl_listView);
        mPullToRefreshListView.init(getActivity(), true);
        mAdapter = new VideoLiveLAdapter(mContext);
        mPullToRefreshListView.setAdapter(mAdapter);
        PackageInfo packageInfo = null;
        try {
            packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext, contentLayout);
        versionCode = packageInfo.versionName;
        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                mNoData.ListViewNoEmpty(mPullToRefreshListView);
                getTuijianList();
            }
        });
        mPullToRefreshListView.setOnLoadDataListener(new PullToRefreshListView.OnLoadDataListener() {
            @Override
            public void onPullRefresh() {

            }

            @Override
            public void onLoadMore() {
                getTuijianList();
            }
        });

    }

    private View findViewById(int viewid) {
        return cv.findViewById(viewid);
    }

    private void getTuijianList() {
        LiveListTask task = new LiveListTask(size, currentPage, null, null, 1, versionCode, "","", "", liveType, "1","1");
        task.addListener(new MyListener(mContext));
        mTuijianListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    class MyListener extends ShowToastTaskEventListener {

        public MyListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (data != null) {
                mTuijianListTaskId = -1;
//                if (mListView.getVisibility() == View.VISIBLE) {
                if (currentPage == 1) {
                    mPullToRefreshListView.onRefreshComplete();
                    mPullToRefreshListView.enableLoadMore();
                    mPullToRefreshListView.setSelection(0);
                } else {
                    mPullToRefreshListView.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                fansList = (List<EatShowUserInfo>) data;
                if (fansList != null && fansList.size() > 0) {
                    mAdapter.updateData(fansList, true);
                }

                currentPage = result;
//                }
            }
            if (mAdapter != null && mAdapter.getCount()== 0){
                mNoData.ListViewEmpty(mPullToRefreshListView,null,null,-1);
            }else {
                mNoData.ListViewNoEmpty(mPullToRefreshListView);
            }

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(EatShowApp.getACache().getAsString(Constants.USE_GUANKANQUAN))){
            String isUse = EatShowApp.getACache().getAsString(Constants.USE_GUANKANQUAN);
            if (isUse.equals("true")){
                getTuijianList();
            }
        }
    }
}
